home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / efn < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  2.6 KB  |  127 lines

  1. #!/bin/ksh
  2. # @(#) efn.ksh 96/01/19
  3. # 93/04/13 john h. dubois iii (john@armory.com)
  4. # 96/01/19 Changed default tmpdir home dir for safety
  5.  
  6. : ${TMP:=$TMPDIR}
  7. : ${TMP:=$HOME}
  8. : ${TMP:=/tmp}
  9. : ${VISUAL:=$EDITOR}
  10. : ${VISUAL:=vi}
  11.  
  12. tmpfile=$TMP/#efn$$
  13. name=${0##*/}
  14. Usage="Usage: $name [-h] [-f <file>] filename ..."
  15. InFile=
  16.  
  17. while getopts :hf: opt; do
  18.     case $opt in
  19.     h)
  20.     echo \
  21. "$name: edit file names.
  22. $Usage
  23. Each file name given is put into a file preceded by a numeric tag to
  24. identify it.  The file is then passed to the user's editor (the value of
  25. \$VISUAL or \$EDITOR if set; vi if not).  The file names may be changed
  26. while in the editor.  The tags should not be changed.  When the editor is
  27. exited, the file is scanned for changed names.  For each name that has
  28. been changed, the name of the file it refers to is changed to the new name.  
  29. Options:
  30. -f: Take file names to be edited from <file> instead of the command line.
  31. -h: print this help."
  32.        exit 0
  33.        ;;
  34.     f)
  35.     if [ ! -r "$OPTARG" ]; then
  36.         echo "$OPTARG: Cannot read."
  37.         exit 1
  38.     fi
  39.     InFile=$OPTARG
  40.     ;;
  41.     +?)
  42.     echo "$name: options should not be preceded by a '+'."
  43.     exit 1
  44.     ;;
  45.     :) 
  46.     print -r -u2 -- \
  47.     "$name: Option '$OPTARG' requires a value.  Use -h for help."
  48.     exit 1
  49.     ;;
  50.     ?) 
  51.     echo "$name: $OPTARG: bad option.  Use -h for help."
  52.     exit 1
  53.     ;;
  54.     esac
  55. done
  56.  
  57. # remove args that were options
  58. let OPTIND=OPTIND-1
  59. shift $OPTIND
  60.  
  61. if [ -n "$InFile" ]; then
  62.     if [ $# -gt 0 ]; then
  63.     echo \
  64. "File names should not be given on the command line if -f <filename> is used."
  65.     exit 1
  66.     fi
  67.     set -- $(<$InFile)
  68. fi
  69.  
  70. if [ $# -lt 1 ]; then
  71.     echo "$Usage\nUse -h for help."
  72.     exit
  73. fi
  74.  
  75. echo "Change each filename to the name the file should be moved to." > $tmpfile
  76. typeset -i sequence=0 numfiles
  77.  
  78. set -A Files -- "$@"
  79.  
  80. trap "rm $tmpfile; exit 0" HUP INT QUIT TERM
  81.  
  82. for file; do
  83.     if [[ -a "$file" ]]; then
  84.     let sequence+=1
  85.     Files[sequence]=$file
  86.     print "$sequence)\t$file"
  87.     else
  88.     print -u2 "$file: No such file."
  89.     fi
  90. done >> $tmpfile
  91.  
  92. numfiles=sequence
  93.  
  94. if [ numfiles -eq 0 ]; then
  95.     rm $tmpfile
  96.     exit 0
  97. fi
  98.  
  99. [[ "$VISUAL" = *vi ]] && VISUAL="$VISUAL +2"
  100.  
  101. [ -s $tmpfile ] && $VISUAL $tmpfile
  102.  
  103. IFS="    "
  104.  
  105. while read tag newname; do
  106.     case "$tag" in
  107.     Change*)
  108.     continue ;;
  109.     +([0-9])\))
  110.     sequence=${tag%\)}
  111.     ;;
  112.     *)
  113.     print -u2 "Editing error: unrecognized tag: $tag"
  114.     continue;;
  115.     esac
  116.     oldname=${Files[sequence]}
  117.     if [ "$oldname" = "$newname" ]; then
  118.     print -- "Not moved: $oldname"
  119.     else
  120.     [[ "$oldname" = -* ]] && oldname="./$oldname"
  121.     print "Moving $oldname to $newname"
  122.     mv "$oldname" "$newname"
  123.     fi
  124. done < $tmpfile
  125. rm $tmpfile
  126. exit 0
  127.